package com.document.controll;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.Cnd;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.document.dao.OrgDao;
import com.document.model.Org;
import com.document.model.PageModel;
import com.document.utils.SystemContext;


@IocBean
@InjectName
public class OrgAction extends BaseAction {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	@Inject
	private OrgDao orgDao = null;
	
	public void setOrgDao(OrgDao orgDao) {
		this.orgDao = orgDao;
	}

	@At("/org/add")
	public void addOrg(@Param("::org.") Org org,@Param("pOrgCode") String pOrgCode,HttpServletResponse resp){
		org.setCreateTime(sdf.format(new Date()));
		int maxCode = orgDao.findMaxOrgCode(pOrgCode);
		if(pOrgCode != null && !"".equals(pOrgCode)){
			org.setOrgCode(pOrgCode + "-" + (maxCode + 1));
		}else{
			org.setOrgCode("" + (maxCode + 1));
		}
		org = basicDao.save(org);
		String rvt = "{success:false}";
		if(org != null && org.getId() > 0){
			rvt = "{success:true,data:" + Json.toJson(org) + "}";
		}
		write(resp,rvt);
	}
	
	@At("/org/update")
	public void updateOrg(@Param("::org.") Org org,HttpServletResponse resp){		
		Org _org = basicDao.find(org.getId(), Org.class);
		_org.setOrgName(org.getOrgName());
		_org.setOrgCode1(org.getOrgCode1());
		_org.setRemark(org.getRemark());
		boolean success = basicDao.update(_org);
		String rvt = "{success:" + success + "}";
		write(resp,rvt);
	}
	
	@At("/org/delete")
	public void deleteOrg(@Param("orgCode") String orgCode,HttpServletResponse resp){
		String rvt = "{success:true}";
		try{
			basicDao.delete("b_org", Cnd.where("orgcode", "like", orgCode + "-%").or("orgcode", "=", orgCode));
		}catch(Exception e){
			rvt = "{success:false}";
		}		
		write(resp,rvt);
	}
	
	
	@At("/org/batchDelete")
	public void batchDeleteOrg(@Param("orgCodes") String[] orgCodes,HttpServletResponse resp){		
//		basicDao.deleteByIds(Org.class,orgIds);
		boolean success = orgDao.batchDeleteOrg(orgCodes);
		String rvt = "{success:" + success + "}";
		write(resp,rvt);
	}
		
	@Ok("jsp:pages.org.orgList")
	@At("/org/listOrg")
	public void list(@Param("currentPage") int currentPage,@Param("orgCode") String orgCode,
			HttpServletRequest req) {
		if(orgCode != null && !"".equals(orgCode)){			
			List<Org> list = basicDao.searchByPage(Org.class, 
					Cnd.where("orgcode", "like", orgCode + "-%"), 
					currentPage, SystemContext.PAGE_SIZE);	
			int len = list.size();
			if(len > 0 && len <= SystemContext.PAGE_SIZE){
				for(int i = list.size();i < SystemContext.PAGE_SIZE;i++){
					list.add(new Org());
				}
			}
			int count = basicDao.searchCount(Org.class,Cnd.where("orgcode", "like", orgCode + "-%"));	
			int maxPage = basicDao.maxPageSize(count, SystemContext.PAGE_SIZE);	
			PageModel<Org> pm = new PageModel<Org>(list, maxPage);
			req.setAttribute("navid", 0);
			req.setAttribute("pm", pm);
			req.setAttribute("orgCode", orgCode);			
		}
		req.setAttribute("ctx", req.getContextPath());
	}
	
	/**
	 * 根据组织机构编码检查该机构是否存在
	 * @param orgCode
	 */
	@At("/org/orgExists")
	public void orgExists(@Param("orgCode") String orgCode,HttpServletResponse resp){
		Org org = basicDao.findByCondition(Org.class, Cnd.where("orgcode1", "=", orgCode));
		String rvt = "{exists:false}";
		if(org != null && org.getOrgCode() != null && !"".equals(org.getOrgCode())){
			rvt = "{exists:true}";
		}
		write(resp,rvt);
	}
	
	@At("/org/findChildOrg")
	public void findChildOrg(HttpServletRequest req,HttpServletResponse resp){
		String orgCode = req.getParameter("orgCode");
		List<Org> orgList = orgDao.findChildOrg(orgCode);
		List<Map<String,String>> treeList = new ArrayList<Map<String,String>>();
		Map<String,String> treeNode = null;
		if(orgCode == null || "".equals(orgCode)){
			treeNode = new HashMap<String,String>();
			treeNode.put("id", "0");
			treeNode.put("name", "部门树");
			treeNode.put("icon", "");
			treeNode.put("isParent", "true");
			treeList.add(treeNode);
		}
		for(Org org : orgList){
			treeNode = new HashMap<String,String>();
			treeNode.put("id", org.getOrgCode());
			treeNode.put("name", org.getOrgName());
			treeNode.put("icon", "");
			treeNode.put("isParent", "true");
			treeNode.put("orgId", org.getId() + "");
			treeNode.put("orgCode", org.getOrgCode());
			if(orgCode == null || "".equals(orgCode)){
				treeNode.put("pId", "0");
			}
			treeList.add(treeNode);
		}
		write(resp,Json.toJson(treeList, JsonFormat.full()));
	}
	@At("/org/findOrgById")
	public void findOrgById(@Param("orgId") int orgId,HttpServletResponse resp){
		Org org = basicDao.find(orgId, Org.class);
		write(resp,Json.toJson(org,JsonFormat.full()));
	}
	
	
}
